package CustomUI;

import com.ydev.dworld3.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;

public class ydButton extends ImageButton {
	
	

	private ImageButton btn;
	
	boolean toggle=false;
	boolean selected=false;
	
	String strAction;
	
	ydOnClickListeners listeners;
	ydOnClickLongListeners longListeners;

	public ydButton(Context context) {
		super(context);

	}
	
	

	public ydButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		xmlAttReader(context, attrs);
		init();
	}

	public ydButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		xmlAttReader(context, attrs);
		init();
	}
	
	public ydButton(Context context, String tag) {
		super(context);
	}
	
	private void xmlAttReader(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.ydButton);

			final int N = a.getIndexCount();
			for (int i = 0; i < N; ++i)
			{
				int attr = a.getIndex(i);
				switch (attr)
				{
					case R.styleable.ydButton_Action:
						strAction = a.getString(attr);
						//...do something with myText...
						break;
					case R.styleable.ydButton_toggle:
						toggle = a.getBoolean(attr, false);
						//...do something with fancyColors...
						break;
					case R.styleable.ydButton_onAction:
						String onAction = a.getString(attr);
						//...we'll setup the callback in a bit...
						break;
				}
			}
			a.recycle();
		
	}

	public void init(){

		btn=this;
		
		listeners = new ydOnClickListeners();
		longListeners = new ydOnClickLongListeners();
		
		listeners.addOnClickListener(new View.OnClickListener(){
			   @Override
			   public void onClick(View v){
				   if(toggle==true){
					   if(selected==true)
						   setState(false);
					   else
						   setState(true);

				   }
			   }
			});
		
		this.setOnClickListener(listeners);
		this.setOnLongClickListener(longListeners);
	}

	public void addOnClickListener(OnClickListener onClickListener) {
		listeners.addOnClickListener(onClickListener);
		
	}
	
	public void addOnLongClickListener(OnLongClickListener onClickListener) {
		longListeners.addOnClickListener(onClickListener);
		
	}
	
	public void setState(Boolean state){
		selected=state;
		btn.setSelected(selected);
	}
	
	public boolean isSelected(){
		return selected;
	}

}
